#!/bin/bash
##################################################################
# Designed by Michael Pan From PARADISE-NCRL-SEU
# All rights reserved!
# You can modify or rewrite this script and release it as you own.
# But, please indicate the creator.
# v0.5
# History
# Version   Author      Date        Comments
# 0.1       Michael Pan 2011-05-23  Create 
# 0.2       Michael Pan 2011-05-28  Change to Dialog
# 0.3       Michael Pan 2011-05-29  Local softwares download
# 0.4       Michael Pan 2011-05-29  modify some bugs
# 0.5       Michael Pan 2011-05-30  delete a local software and modify some bugs
##################################################################
VERSION=0.5
# root is needed to run this script
ROOT_UID=0
ERROR_NOT_ROOT=67
DIALOG=${DIALOG="whiptail"}
DIALOG_OK=${DIALOG_OK="0"}
DIALOG_CANCEL=${DIALOG_CANCEL="1"}
DIALOG_ESC=${DIALOG_ESC="255"}
tempfile=`tempfile 2>/dev/null` || tempfile=/tmp/StartUbuntu$$
Switch_ConfigPPPoE=no
Switch_ConfigBras=no
Switch_ConfigDefaultBootOption=no
Switch_ConfigSources=no
Switch_ConfigChinese=no
Switch_ConfigVim=no
Switch_InstallSunJava=no
Switch_InstallFlash=no
Switch_InstallSoftwaresFromSource=no
Switch_InstallLocalSoftwares=no
ARCH=`arch`
CODENAME=`lsb_release -c`
CODENAME="${CODENAME:10}"
RELEASE=`lsb_release -r`
RELEASE="${RELEASE:9}"

if [ "$UID" -ne "$ROOT_UID" ]; then
	echo "Must be root to run this script."
	echo "Please use \"sudo\" or \"su\" to start over."
	exit $ERROR_NOT_ROOT
fi

trap "rm -f $tempfile" 0 1 2 5 15

# cd to the directory contains all the scripts
CURRENT_DIR=`dirname $0`
cd "$CURRENT_DIR"
CURRENT_DIR=`pwd`
YES="$CURRENT_DIR/yes"
NO="$CURRENT_DIR/no"
echo "yes" > "$YES"
echo "no" > "$NO"
chmod a+rw "$YES"
chmod a+rw "$NO"

if [ -x "/usr/bin/dialog" ];then
	DIALOG=${DIALOG="dialog"}
fi

$DIALOG --title "Welcome to StartUbuntu" --clear \
	--msgbox "\n\
	Welcome to use StartUbuntu to configure your Ubuntu $RELEASE $CODENAME!\n\
	This script is designed by Michael Pan\n\
	From PARADISE-NCRL-SEU\n\
	You can modify or rewrite this script and release it as you own.\n\
	But, please indicate the creator in the source code.\n\
	MSN: panleyuan@hotmail.com\n\
	Bug Report: leyuanpan@gmail.com\n\
	2011-05-23\n\
	Enjoy It!\n" 18 80;
#options of downloading attachments or not
ATT_SRC_1="http://bbs.ncrl.3322.org/download/startubuntu"
ATT_SRC_2="http://ubuntu.ncrl.3322.org/startubuntu"
ATT_SRC_3="http://code.google.com/p/startubuntu/downloads/list"
$DIALOG --title "Start Ubuntu Configuration" \
	--clear \
	--separate-output \
	--radiolist "\nNow, please tell me if you want to download the attachment or not. If you want to download the attachments, please tell me the source address. If you do not want to download it, please make sure that the attachment exits in '$CURRENT_DIR'.\nUse the UP/DOWN arrow keys to choose an option. Press SPACE to toggle an option ON/OFF. Press TAB to choose OK or CANCEL.\n"\
	20 80 4 \
	"1"  "$ATT_SRC_1" on \
	"2"  "$ATT_SRC_2" off \
	"3"  "$ATT_SRC_3" off \
	"4"  "Do not download" off \
	2> $tempfile

retval=$?
choice=`cat $tempfile`
if [[ "$retval" = "$DIALOG_OK" ]];then
	case "$choice" in
		"1")
			Switch_Attach_Src=1
			;;
		"2")
			Switch_Attach_Src=2
			;;
		"3")
			Switch_Attach_Src=3
			;;
		*)
			Switch_Attach_Src=4
			;;
	esac
else
	echo "Done with Cancel!"
	exit 0
fi
#configure installing options
$DIALOG --title "Start Ubuntu Configuration" \
	--clear \
	--separate-output \
	--checklist "\nNow, you need to choose what you want to configure and which softwares you want to install.\nUse the UP/DOWN arrow keys to choose an option. Press SPACE to toggle an option ON/OFF. Press TAB to choose OK or CANCEL.\n"\
	20 80 8 \
	"1"  "Configure PPPoE" on \
	"2"  "Install and Configure bras" on \
	"3"  "Configure default boot option" off \
	"4"  "Configure apt-get sources" on \
	"5"  "Configure Chinese environment" on \
	"6"  "Configure Matlab's Chinese environment and icons" off \
	"7"  "Configure Sun Java" off \
	"8"  "Install Sun Java" off \
	"9"  "Install Flash plugin for Internet browsers                  " on \
	"10"  "Install softwares from online sources" on \
	"11"  "Install local softwares" on \
	"12"  "Install Gnome3" off \
	2> $tempfile

retval=$?  
choices=`cat $tempfile`
if [ "$choices" = "" ];then
	echo "Done with no options chosen"
	exit 0
fi
case $retval in
	$DIALOG_OK)
		for choice in $choices
		do
			case $choice in
				"1")
					Switch_ConfigPPPoE=yes
					;;
				"2")
					Switch_ConfigBras=yes
					;;
				"3")
					Switch_ConfigDefaultBootOption=yes
					;;
				"4")
					Switch_ConfigSources=yes
					;;
				"5")
					Switch_ConfigChinese=yes
					;;
				"6")
					Switch_ConfigMatlab=yes
					;;
				"7")
					Switch_ConfigSunJava=yes
					;;
				"8")
					Switch_InstallSunJava=yes
					;;
				"9")
					Switch_InstallFlash=yes
					;;
				"10")
					Switch_InstallSoftwaresFromSource=yes
					;;
				"11")
					Switch_InstallLocalSoftwares=yes
					;;
				"12")
					Switch_InstallGnome3=yes
					;;
			esac
		done
		;;
	$DIALOG_CANCEL)
		echo "Done with Cancel"
		exit 0
		;;
	$DIALOG_ESC)
		echo "Done with Cancel"
		exit 0
		;;
	*)
		echo "Unexpected return code: $retval (ok would be $DIALOG_OK)"
		exit -1
		;;
esac

#configure softwares installed from source options
if [[ "$Switch_InstallSoftwaresFromSource" = "yes" ]];then
	$DIALOG --title "Options of Softwares to be Installed from Online Source" \
		--clear \
		--separate-output \
		--checklist "\nNow, you need to choose what you want to configure and which softwares you want to install.\nUse the UP/DOWN arrow keys to choose an option. Press SPACE to toggle an option on/off. Press TAB to choose OK or CANCEL.\n"\
		20 80 8 \
		"1"  "Nautilus open termial in right popup menu" on \
		"2"  "Build essential for kernel compiling environment" on \
		"3"  "Samba -- for LAN file share with Windows OS" on \
		"4"  "VIM -- a great text and sources editor." on \
		"5"  "VIM-Gnome -- gVim for gnome environment." on \
		"6"  "VIM-Gtk -- gVim for GTK environment" on \
		"7"  "Ctags -- a great plugin for Vim for high efficient coding" on \
		"8"  "Cscope -- a greater plugin for Vim" on \
		"9"  "Anjuta -- a useful IDE in Linux platform" on \
		"10"  "Compression support suit" on \
		"11"  "Mplayer -- a great media player for Linux OS" on \
		"12"  "Smplayer -- a GUI front for mplayer" on \
		"13"  "VLC -- a good media player" on \
		"14"  "Media codecs suit" on \
		"15"  "Amsn -- a MSN client in Linux" on \
		"16"  "Pidgin -- a general IM client" on \
		"17"  "Gimp -- image processing in Linux like photoshop" on \
		"18"  "Deja-dup -- backup data? Choose me!" on \
		"19"  "Openshot -- edit your videos in Linux" on \
		"20"  "Vuze -- a popular BT client" on \
		"21"  "Deluge -- another BT client" on \
		"22"  "Filezilla -- a perfect FTP client" on \
		"23"  "Gftp -- a popular FTP client" on \
		"24"  "Vsftp -- a perfect FTP server" off \
		"25"  "wget -- a general downloading tool" on \
		"26"  "Multiget -- a downloader with friendly GUI front" on \
		"27"  "D4x -- download for X-server" off \
		"28"  "Amule -- emule's brother in Linux platform" on \
		"29"  "Xpdf -- a good pdf viewer in Linux" on \
		"30"  "Chmsee -- a chm viewer in Linux" on \
		"31"  "Chromium -- Google's fast Internet browser" on \
		"32"  "Stardict -- a perfect dictionary in Linux" on \
		"33"  "Ia32-libs -- if your OS is x86_64, choose me!" on \
		"34"  "TeXLive -- oh, if you don't know me, out!" on \
		"35"  "Eclipse -- a great open source IDE" on \
		"36"  "Netbeans -- another great open source IDE" on \
		"37"  "Wesnoth -- a great strategy game in Linux." on \
		"38"  "Wormux -- a good game for entertaiment" on \
		"39"  "Hedgewars -- just like Wormux" on \
		"40"  "Frozen-Bubble -- a game for girls" on \
		"41"  "Tremulous -- I don't know about this game!" on \
		"42"  "Neverball -- a good 3D game just like Balance in Windows" on \
		"43"  "Gnome-RDP -- remote desktop client in Gnome to Windows" on \
		2> $tempfile

	retval=$?
	choices=`cat $tempfile`
	if [[ ("$retval" = "$DIALOG_OK") && ("$choices" != "") ]];then
		for choice in $choices
		do
			case $choice in
				"1")
					Switch_InstallOpenTerminal=yes
					;;
				"2")
					Switch_InstallBuildEssential=yes
					;;
				"3")
					Switch_InstallSamba=yes
					;;
				"4")
					Switch_InstallVim=yes
					Switch_ConfigVim=yes
					;;
				"5")
					Switch_InstallVimGnome=yes
					;;
				"6")
					Switch_InstallVimGtk=yes
					;;
				"7")
					Switch_InstallCtags=yes
					;;
				"8")
					Switch_InstallCscope=yes
					;;
				"9")
					Switch_InstallAnjuta=yes
					;;
				"10")
					Switch_InstallCompressionSupport=yes
					;;
				"11")
					Switch_InstallMplayer=yes
					;;
				"12")
					Switch_InstallSmplayer=yes
					;;
				"13")
					Switch_InstallVLC=yes
					;;
				"14")
					Switch_InstallMediaCodecs=yes
					;;
				"15")
					Switch_InstallAmsn=yes
					;;
				"16")
					Switch_InstallPidgin=yes
					;;
				"17")
					Switch_InstallGimp=yes
					;;
				"18")
					Switch_InstallDeja=yes
					;;
				"19")
					Switch_InstallOpenshot=yes
					;;
				"20")
					Switch_InstallVuze=yes
					;;
				"21")
					Switch_InstallDeluge=yes
					;;
				"22")
					Switch_InstallFilezilla=yes
					;;
				"23")
					Switch_InstallGftp=yes
					;;
				"24")
					Switch_InstallVsftp=yes
					;;
				"25")
					Switch_InstallWget=yes
					;;
				"26")
					Switch_InstallMultiget=yes
					;;
				"27")
					Switch_InstallD4x=yes
					;;
				"28")
					Switch_InstallAmule=yes
					;;
				"29")
					Switch_InstallXpdf=yes
					;;
				"30")
					Switch_InstallChmsee=yes
					;;
				"31")
					Switch_InstallChrome=yes
					;;
				"32")
					Switch_InstallStarDict=yes
					;;
				"33")
					Switch_InstallIa32Libs=yes
					;;
				"34")
					Switch_InstallTeXLive=yes
					;;
				"35")
					Switch_InstallEclipse=yes
					;;
				"36")
					Switch_InstallNetbeans=yes
					;;
				"37")
					Switch_InstallWesnoth=yes
					;;
				"38")
					Switch_InstallWormux=yes
					;;
				"39")
					Switch_InstallHedgewars=yes
					;;
				"40")
					Switch_InstallFrozenBubble=yes
					;;
				"41")
					Switch_InstallTremulous=yes
					;;
				"42")
					Switch_InstallNeverball=yes
					;;
				"43")
					Switch_InstallGnomeRDP=yes
					;;
			esac
		done
	else
		Switch_InstallSoftwaresFromSource=no
	fi
fi

#Options of Local Softwares to be Installed
if [[ "$Switch_InstallLocalSoftwares" = "yes" ]];then
	$DIALOG --title "Options of Local Softwares to be Installed" \
		--clear \
		--separate-output \
		--checklist "\nPlease select what you want to install in your system from local softwares archives.\nUse the UP/DOWN arrow keys to choose an option. Press SPACE to toggle an option ON/OFF. Press TAB to choose OK or CANCEL.\n"\
		18 80 6 \
		"1"  "Adobe Reader for Linux" on \
		"2"  "Tecent QQ client for Linux x86 only" off \
		"3"  "W32codecs for Linux x86 only" on \
		"4"  "W64codecs for Linux x86_64 only" off \
		"5"  "Real Player 11 for Linux x86 only                        " on \
		2> $tempfile

	retval=$?  
	choices=`cat $tempfile`
	if [[ ("$retval" = "$DIALOG_OK") && ("$choices" != "") ]];then
		for choice in $choices
		do
			case $choice in
				"1")
					Switch_InstallAdobeReader=yes
					;;
				"2")
					Switch_InstallLinuxQQ=yes
					;;
				"3")
					Switch_InstallW32Codecs=yes
					;;
				"4")
					Switch_InstallW64Codecs=yes	
					;;
				"5")
					Switch_InstallRealPlayer=yes
					;;
			esac
		done
	else
		Switch_InstallLocalSoftwares=no
	fi
fi

# configure PPPoE
if [[ "$Switch_ConfigPPPoE" = "yes" ]];then
	pppoeconf
fi

# configure bras
if [[ "$Switch_ConfigBras" = "yes" ]];then
	L2TPD_ETC_DIR="/etc/l2tpd"
	BRAS_ETC_DIR="/etc/bras"
	BRAS_CONF="$BRAS_ETC_DIR/bras.conf"
	if [ ! -d "$L2TPD_ETC_DIR" ];then
		mkdir "$L2TPD_ETC_DIR"
	fi
	if [ ! -d "$BRAS_ETC_DIR" ];then
		mkdir "$BRAS_ETC_DIR"
	fi
	$DIALOG --title "Installing Bras" \
		--clear \
		--inputbox "Please enter your student ID (without \"@a\" or \"@b\"):" 10 60 2> $tempfile

	retval=$?

	case $retval in
		$DIALOG_OK)
			student_id=`cat $tempfile`
			echo "$student_id" > $BRAS_CONF
			;;
		*)
			echo "#student id" > $BRAS_CONF
			;;
	esac

	$DIALOG --title "Installing Bras" \
		--clear \
		--passwordbox "Please enter your password for bras:" 10 60 2> $tempfile 

	retval=$?

	case $retval in
		$DIALOG_OK)
			password=`cat $tempfile`
			echo "$password" >> $BRAS_CONF
			;;
		*)
			echo "#password" >> $BRAS_CONF
			;;
	esac

	$DIALOG --title "Installing Bras" \
		--clear \
		--inputbox "Please enter your gateway address:" 10 60 2> $tempfile 

	retval=$?

	case $retval in
		$DIALOG_OK)
			gateway=`cat $tempfile`
			echo "$gateway" >> $BRAS_CONF
			;;
		*)
			echo "#gateway" >> $BRAS_CONF
			;;
	esac

	$DIALOG --title "Installing Bras" \
		--clear \
		--yesno "If the information you entered above is wrong or you want to modify them, please edit /etc/bras/bras.conf to change them to right ones.\n\nConnect to Bras after installing?" 12 60;

	retval=$?

	case $retval in
		$DIALOG_OK)
			Switch_DialBras="yes"
			;;
		*)
			Switch_DialBras="no"
			;;
	esac
fi
# download attachments
case "$Switch_Attach_Src" in
	"1")
		if [[ (-d "softwares") && (-d "fonts") && (-d "png") && (-d "vim") ]];then
			:
		else
			wget "$ATT_SRC_1/StartUbuntu-v$VERSION-attachment.tar.bz2" -O "StartUbuntu-v$VERSION-attachment.tar.bz2"
		fi
		;;
	"2")
		if [[ (-d "softwares") && (-d "fonts") && (-d "png") && (-d "vim") ]];then
			:
		else
			wget "$ATT_SRC_2/StartUbuntu-v$VERSION-attachment.tar.bz2" -O "StartUbuntu-v$VERSION-attachment.tar.bz2"
		fi
		;;
	"3")
		if [[ (-d "softwares") && (-d "fonts") && (-d "png") && (-d "vim") ]];then
			:
		else
			wget "http://startubuntu.googlecode.com/files/StartUbuntu-v$VERSION-attachment-1.tar.bz2" -O "StartUbuntu-v$VERSION-attachment-1.tar.bz2"
			wget "http://startubuntu.googlecode.com/files/StartUbuntu-v$VERSION-attachment-2.tar.bz2" -O "StartUbuntu-v$VERSION-attachment-2.tar.bz2"
		fi
		;;
	*)
		;;
esac

if [[ (-d "softwares") && (-d "fonts") && (-d "png") && (-d "vim") ]];then
	:
elif [[ -e "StartUbuntu-v$VERSION-attachment.tar.bz2" ]];then
	chmod a+rw "StartUbuntu-v$VERSION-attachment.tar.bz2"
	tar -xjvf "StartUbuntu-v$VERSION-attachment.tar.bz2"
elif [[ (-e "StartUbuntu-v$VERSION-attachment-1.tar.bz2") && (-e "StartUbuntu-v$VERSION-attachment-2.tar.bz2") ]];then
	chmod a+rw "StartUbuntu-v$VERSION-attachment-1.tar.bz2"
	chmod a+rw "StartUbuntu-v$VERSION-attachment-2.tar.bz2"

	tar -xjvf "StartUbuntu-v$VERSION-attachment-1.tar.bz2"
	tar -xjvf "StartUbuntu-v$VERSION-attachment-2.tar.bz2"
else
	$DIALOG --title "Start Ubuntu Configuration" \
		--clear \
		--yesno "$CURRENT_DIR/StartUbuntu-v$VERSION-attachment.tar.bz2 does not exit!\nContinue?" 12 60;

	retval=$?

	if [[ "$retval" != "$DIALOG_OK" ]];then
		echo "Done with Cancel!"
		exit 0
	fi
fi

#install bras
if [[ "$Switch_ConfigBras" = "yes" ]];then
	cp $CURRENT_DIR/softwares/xl2tpd-1.1.05.tar.gz /usr/local/src
	cd /usr/local/src
	tar zxvf xl2tpd-1.1.05.tar.gz 
	cd xl2tpd-1.1.05
	make
	make install

	cp $CURRENT_DIR/softwares/linux-seubras-0.2.tar.gz /usr/local/sbin
	cd /usr/local/sbin
	tar zxvf linux-seubras-0.2.tar.gz
	cd linux-seubras-0.2
	./bras install

	cp /usr/local/sbin/linux-seubras-0.2/bras /usr/sbin/
	cd /usr/sbin

	chown root.root bras
	ln -sf bras brasa
	ln -sf bras brasb
	chown root.root bras
	chmod 755 bras

	cd "$CURRENT_DIR"
fi

if [[ "$Switch_DialBras" = "yes" ]];then
	$DIALOG --title "Dial Bras" \
		--clear \
		--radiolist "\nChoose Bras@a or Bras@b?\n"\
		12 60 2 \
		"a"  "$student_id@a                              " off \
		"b"  "$student_id@b" on \
		2> $tempfile

	retval=$?  
	choice=`cat $tempfile`
	if [[ "$retval" = "$DIALOG_OK" ]];then
		if [[ "$choice" = "a" ]];then
			brasa
		else
			brasb
		fi
		sleep 3
	fi
fi

#configure default boot option
if [[ "$Switch_ConfigDefaultBootOption" = "yes" ]];then
	echo "Waiting for closing the editor ..."
	gedit /etc/default/grub
fi

#configure apt-get sources
if [[ "$Switch_ConfigSources" = "yes" ]];then
	#the file that contains sources
	local_source_file="sources.profile"
	system_source="/etc/apt/sources.list"
	system_source_bak="/etc/apt/sources.list.bak"

	if [[ -s "$system_source" ]];then
		mv $system_source $system_source_bak
	fi

	if [ ! -s "$local_source_file" ];then
		echo "No valid source file found! Use default:"
		echo "# sources from USTC" >> $system_source
		echo "deb http://ubuntu.ncrl.3322.org/ubuntu/ $CODENAME main multiverse restricted universe" >> $system_source
		echo "deb http://ubuntu.ncrl.3322.org/ubuntu/ $CODENAME-backports main multiverse restricted universe" >> $system_source
		echo "deb http://ubuntu.ncrl.3322.org/ubuntu/ $CODENAME-proposed main multiverse restricted universe" >> $system_source
		echo "deb http://ubuntu.ncrl.3322.org/ubuntu/ $CODENAME-security main multiverse restricted universe" >> $system_source
		echo "deb http://ubuntu.ncrl.3322.org/ubuntu/ $CODENAME-updates main multiverse restricted universe" >> $system_source
		echo "deb-src http://ubuntu.ncrl.3322.org/ubuntu/ $CODENAME main multiverse restricted universe" >> $system_source
		echo "deb-src http://ubuntu.ncrl.3322.org/ubuntu/ $CODENAME-backports main multiverse restricted universe" >> $system_source
		echo "deb-src http://ubuntu.ncrl.3322.org/ubuntu/ $CODENAME-proposed main multiverse restricted universe" >> $system_source
		echo "deb-src http://ubuntu.ncrl.3322.org/ubuntu/ $CODENAME-security main multiverse restricted universe" >> $system_source
		echo "deb-src http://ubuntu.ncrl.3322.org/ubuntu/ $CODENAME-updates main multiverse restricted universe" >> $system_source
	else
		while read line
		do
			echo "$line" >> $system_source
		done < "$local_source_file"
	fi
	apt-get update
	dpkg --configure -a
	apt-get upgrade < "$YES"
fi
# configure Chinese environment
if [[ "$Switch_ConfigChinese" = "yes" ]];then
	# install Chinese fonts
	FONT_DIR="/usr/share/fonts/truetype"
	cp fonts/*.ttf "$FONT_DIR"
	cd "$FONT_DIR"
	chmod a+r *.ttf
	mkfontscale
	mkfontdir
	fc-cache

	zh_hans="/var/lib/locales/supported.d/zh-hans"
	echo >> "$zh_hans"
	echo "zh_CN.GB18030 GB18030" >> "$zh_hans"
	echo "zh_CN.GBK GBK" >> "$zh_hans"
	echo "zh_CN.GB2312 GB2312" >> "$zh_hans"
	echo "zh_HK.BIG5 BIG5" >> "$zh_hans"
	echo "zh_TW.BIG5 BIG5" >> "$zh_hans"
	locale-gen

	# configure gedit Chinese environment for current user
	ENCODING_ROOT="$HOME/.gconf/apps/gedit-2/preferences/encodings"
	if [[ -d "$ENCODING_ROOT" ]];then
		:
	else
		mkdir -p "$ENCODING_ROOT"
		chmod a+rw "$ENCODING_ROOT"
	fi
	gconf_encoding="$ENCODING_ROOT/%gconf.xml"
	gconf_encoding_bak="$ENCODING_ROOT/%gconf.xml.bak"
	if [[ -e "$gconf_encoding" ]];then
		cp "$gconf_encoding" "$gconf_encoding_bak"
		chmod a+rw "$gconf_encoding_bak"
	fi
	chmod a+rw "$gconf_encoding"
	echo '<?xml version="1.0"?>' > "$gconf_encoding"
	echo '<gconf>' >> "$gconf_encoding"
	echo "<entry name=\"auto_detected\" mtime=\"`date +%s`\" type=\"list\" ltype=\"string\">" >> "$gconf_encoding"
	for coding in "GB18030" "GBK" "GB2312" "UTF-8" "CURRENT" "BIG5" "ISO-8859-15" "UTF-16"
	do
		echo '<li type="string">' >> "$gconf_encoding"
		echo "<stringvalue>$coding</stringvalue>" >> "$gconf_encoding"
		echo '</li>' >> "$gconf_encoding"
	done
	echo '</entry>' >> "$gconf_encoding"
	echo '</gconf>' >> "$gconf_encoding"

	# configure gedit Chinese environment for root
	ENCODING_ROOT="/root/.gconf/apps/gedit-2/preferences/encodings"
	if [[ -d "$ENCODING_ROOT" ]];then
		:
	else
		mkdir -p "$ENCODING_ROOT"
		chmod +rw "$ENCODING_ROOT"
	fi
	gconf_encoding="$ENCODING_ROOT/%gconf.xml"
	gconf_encoding_bak="$ENCODING_ROOT/%gconf.xml.bak"
	if [[ -e "$gconf_encoding" ]];then
		cp "$gconf_encoding" "$gconf_encoding_bak"
	fi
	echo '<?xml version="1.0"?>' > "$gconf_encoding"
	echo '<gconf>' >> "$gconf_encoding"
	echo "<entry name=\"auto_detected\" mtime=\"`date +%s`\" type=\"list\" ltype=\"string\">" >> "$gconf_encoding"
	for coding in "GB18030" "GBK" "GB2312" "UTF-8" "CURRENT" "BIG5" "ISO-8859-15" "UTF-16"
	do
		echo '<li type="string">' >> "$gconf_encoding"
		echo "<stringvalue>$coding</stringvalue>" >> "$gconf_encoding"
		echo '</li>' >> "$gconf_encoding"
	done
	echo '</entry>' >> "$gconf_encoding"
	echo '</gconf>' >> "$gconf_encoding"

fi

# configure Matlab Icon and Chinese environment
if [[ "$Switch_ConfigMatlab" = "yes" ]];then
	subdir="sys/java/jre/glnx*/jre*/lib/fonts";
	$DIALOG --title "Configure Matlab" \
		--clear \
		--default-item "\opt\Matlab\R2011a" \
		--inputbox "Please tell me where your Matlab is installed:" 10 60 2> $tempfile

	retval=$?

	if [[ "$retval" = "$DIALOG_OK" ]];then
		input=`cat $tempfile`
		dir="$input/$subdir"
		while [[ ! -d "$dir" ]];do
			$DIALOG --title "Configure Matlab" \
				--clear \
				--default-item "\opt\Matlab\R2011a" \
				--inputbox "\"$input\" is not a valid Matlab root directory. Try again.\nPlease tell me where your Matlab is installed:" 10 60 2> $tempfile
			retval=$?
			if [[ "$retval" = "$DIALOG_OK" ]];then
				input=`cat $tempfile`
				dir="$input/$subdir"
			else
				input="cancel"
				break
			fi
		done
		if [[ "$input" != "cancel" ]];then
			if [[ -e "png/matlab.png" ]];then
				cp png/matlab.png "$input"

				ln -sf "$input/bin/matlab" "/usr/bin/matlab"
				DESKTOP_ENTRY="/usr/share/applications/matlab.desktop"
				echo '[Desktop Entry]' > "$DESKTOP_ENTRY"
				echo 'Name=Matlab' >> "$DESKTOP_ENTRY"
				echo 'Comment=Mathworks Matlab for Linux' >> "$DESKTOP_ENTRY"
				echo "Exec=$input/bin/matlab -desktop" >> "$DESKTOP_ENTRY"
				echo "Icon=$input/matlab.png" >> "$DESKTOP_ENTRY"
				echo 'Terminal=false' >> "$DESKTOP_ENTRY"
				echo 'Type=Application' >> "$DESKTOP_ENTRY"
				echo 'Categories=Application;Development;' >> "$DESKTOP_ENTRY"
			fi
			cd "$dir"
			mkdir fallback
			cp $CURRENT_DIR/fonts/*.ttf fallback
			cd fallback
			chmod +r *.ttf
			mkfontscale
			cd ..
			chmod +w fonts.scale
			cat fallback/fonts.scale >> fonts.scale
			cd "CURRENT_DIR"
		fi
	fi
fi

# install Sun Java
if [[ "$Switch_InstallSunJava" = "yes" ]];then
	add-apt-repository ppa:ferramroberto/java
	apt-get update
	apt-get install sun-java6-jdk sun-java6-plugin < "$YES"
	add-apt-repository -r ppa:ferramroberto/java
fi

# configure sun Java Chinese environment
if [[ "$Switch_ConfigSunJava" = "yes" ]];then
	#first configure sun-java Chinese environment
	apt-get install ttf-wqy-zenhei < "$YES"
	mkdir /usr/lib/jvm/java-6-sun/jre/lib/fonts/fallback
	sudo ln -s /usr/share/fonts/truetype/wqy/wqy-zenhei.ttc /usr/lib/jvm/java-6-sun/jre/lib/fonts/fallback/wqy-zenhei.ttc
	#second choose default java
	echo "If there are two choices, please choose sun-java."
	update-alternatives --config java
	update-alternatives --config javac
	#third configure global variable environment
	ENV_FILE="/etc/environment"
	cp "$ENV_FILE" "$ENV_FILE.old"

	. /etc/environment
	if [ "${PATH:0:27}" = "/usr/lib/jvm/java-6-sun/bin" ];then
		echo "PATH=\"$PATH\"" > "$ENV_FILE"
	else
		echo "PATH=\"/usr/lib/jvm/java-6-sun/bin:$PATH\"" > "$ENV_FILE"
	fi
	echo "JAVA_HOME=/usr/lib/jvm/java-6-sun" >> "$ENV_FILE"
	echo "CLASSPATH=.:/usr/lib/jvm/java-6-sun/lib" >> "$ENV_FILE"
	while read line
	do
		if [ "${line:0:4}" != "PATH" ];then
			if [ "${line:0:9}" != "JAVA_HOME" ];then
				if [ "${line:0:9}" != "CLASSPATH" ];then
					echo "$line" >> "$ENV_FILE"
				fi
			fi
		fi
	done < "$ENV_FILE.old"
	echo "Done!"
	. /etc/environment
fi

# install Flash plugin for browsers
if [[ "$Switch_InstallFlash" = "yes" ]];then
	if [ "$ARCH" = "x86_64" ];then
		add-apt-repository ppa:sevenmachines/flash
		apt-get update
		apt-get install flashplugin64-installer < "$YES"
		add-apt-repository -r ppa:sevenmachines/flash
		apt-get update
	else
		apt-get install flashplugin-installer < "$YES"
	fi
fi
# install softwares from online sources
if [[ "$Switch_InstallSoftwaresFromSource" = "yes" ]];then
	#install open terminal in popup menu
	if [[ "$Switch_InstallOpenTerminal" = "yes" ]];then
		apt-get install nautilus-open-terminal < "$YES"
	fi

	#install build-essential environment for kernel compile
	if [[ "$Switch_InstallBuildEssential" = "yes" ]];then
		apt-get install module-assistant < "$YES"
		apt-get install build-essential < "$YES"
		apt-get install fakeroot < "$YES"
		apt-get install dh-make < "$YES"
		apt-get install debhelper < "$YES"
		apt-get install debconf < "$YES"
		apt-get install libstdc++6 < "$YES"
		apt-get install linux-headers-$(uname -r) < "$YES"
	fi

	#install samba for file sharing with windows
	if [[ "$Switch_InstallSamba" = "yes" ]];then
		apt-get install samba < "$YES"
	fi

	#install vim and gvim
	if [[ "$Switch_InstallVim" = "yes" ]];then
		apt-get install vim < "$YES"
	fi
	if [[ "$Switch_InstallVimGnome" = "yes" ]];then
		apt-get install vim-gnome < "$YES"
	fi
	if [[ "$Switch_InstallVimGtk" = "yes" ]];then
		apt-get install vim-gtk < "$YES"
	fi
	if [[ "$Switch_InstallCtags" = "yes" ]];then
		apt-get install ctags < "$YES"
	fi
	if [[ "$Switch_InstallCscope" = "yes" ]];then
		apt-get install cscope < "$YES"
	fi

	#configure Vim
	if [[ "$Switch_ConfigVim" = "yes" ]];then
		#copy plugins to vim
		cp -R vim/ColorSamplerPack/* /usr/share/vim/vim7*
		cp -R vim/csExplorer/* /usr/share/vim/vim7*
		cp -R vim/matlab/* /usr/share/vim/vim7*
		cp -R vim/NERD_tree/* /usr/share/vim/vim7*
		cp -R vim/taglist_*/* /usr/share/vim/vim7*
		cp -R vim/vim-autocomplpop/* /usr/share/vim/vim7*
		cp vim/*.vim /usr/share/vim/vim7*/plugin
		cp vim/neocomplcache-*.vba /usr/share/vim/vim7*/plugin
		cp -R vim/vim-latex-*/* /usr/share/vim/vim7*
		cp vim/vim-latex-*/*tags /usr/share/vim/vim7*
		cp vim/vim-latex-*/Makefile* /usr/share/vim/vim7*

		#configure vimrc
		cp /etc/vim/vimrc /etc/vim/vimrc.old
		cp vim/vimrc.linux /etc/vim/vimrc

		#configure neocomplcache.vba
		#echo 'Please enter ":so %" and execute it in Vim'
		#vim /usr/share/vim/vim7*/plugin/neocomplcache-*.vba

		#index system header files
		cd /usr/include
		ctags -R
		#install anjuta IDE
		if [[ "$Switch_InstallAnjuta" = "yes" ]];then
			apt-get install anjuta < "$YES"
		fi
	fi

	#install compression format support in ubuntu
	if [[ "$Switch_InstallCompressionSupport" = "yes" ]];then
		apt-get install rar < "$YES"
		apt-get install unrar < "$YES"
		apt-get install p7zip-full < "$YES"
		apt-get install p7zip-rar < "$YES"
		apt-get install zip < "$YES"
		apt-get install unzip < "$YES"
		apt-get install unace < "$YES"
		apt-get install sharutils < "$YES"
		apt-get install uudeview < "$YES"
		apt-get install mpack < "$YES"
		apt-get install lha < "$YES"
		apt-get install arj < "$YES"
		apt-get install cabextract < "$YES"
		apt-get install file-roller < "$YES"
	fi

	#some media players
	if [[ "$Switch_InstallMplayer" = "yes" ]];then
		apt-get install mplayer < "$YES"
	fi
	if [[ "$Switch_InstallSmplayer" = "yes" ]];then
		apt-get install smplayer < "$YES"
	fi
	if [[ "$Switch_InstallVLC" = "yes" ]];then
		apt-get install vlc < "$YES"
	fi

	#some codec support for media players
	if [[ "$Switch_InstallMediaCodecs" = "yes" ]];then
		apt-get install libxine1-ffmpeg < "$YES"
		apt-get install gxine < "$YES"
		apt-get install mencoder < "$YES"
		apt-get install libquicktime1 < "$YES"
		apt-get install flac < "$YES"
		apt-get install faac < "$YES"
		apt-get install sox < "$YES"
		apt-get install ffmpeg2theora < "$YES"
		apt-get install libmpeg2-4 < "$YES"
		apt-get install uudeview < "$YES"
		apt-get install libmpeg3-1 < "$YES"
		apt-get install mpeg3-utils < "$YES"
		apt-get install mpegdemux < "$YES"
		apt-get install liba52-dev < "$YES"
		apt-get install mpeg2dec < "$YES"
		apt-get install vorbis-tools < "$YES"
		apt-get install id3v2 < "$YES"
		apt-get install mpg321 < "$YES"
		apt-get install mpg123 < "$YES"
		apt-get install libflac++6 < "$YES"
		apt-get install ffmpeg < "$YES"
		apt-get install libmp4v2-0 < "$YES"
		apt-get install totem-mozilla < "$YES"
		apt-get install icedax < "$YES"
		apt-get install tagtool < "$YES"
		apt-get install easytag < "$YES"
		apt-get install id3tool < "$YES"
		apt-get install lame < "$YES"
		apt-get install  nautilus-script-audio-convert < "$YES"
		apt-get install libmad0 < "$YES"
		apt-get install libjpeg-progs < "$YES"
		apt-get install gstreamer0.10-ffmpeg < "$YES"
		apt-get install gstreamer0.10-plugins-bad < "$YES"
		apt-get install gstreamer0.10-plugins-bad-multiverse < "$YES"
		apt-get install gstreamer0.10-plugins-ugly < "$YES"
		apt-get install gstreamer0.10-plugins-ugly-multiverse < "$YES"
		#DVD development supported
		apt-get install libdvd-dev < "$YES"
	fi

	#some IM clients
	#for MSN
	if [[ "$Switch_InstallAmsn" = "yes" ]];then
		apt-get install amsn < "$YES"
	fi
	#pidgin
	if [[ "$Switch_InstallPidgin" = "yes" ]];then
		apt-get install pidgin < "$YES"
		apt-get install pidgin-musictracker < "$YES"
		apt-get install pidgin-themes < "$YES"
		#pidgin plugin for China Mobile Fetion
		apt-get install pidgin-openfetion < "$YES"
		apt-get install pidgin-encryption < "$YES"
		apt-get install pidgin-festival < "$YES"
		apt-get install pidgin-hotkeys < "$YES"
		apt-get install pidgin-plugin-pack < "$YES"
		#pidgin plugin for skype
		apt-get install pidgin-skype < "$YES"
		#pidgin plugin for twitter
		apt-get install pidgin-twitter < "$YES"
	fi
	#for image processing
	if [[ "$Switch_InstallGimp" = "yes" ]];then
		apt-get install gimp < "$YES"
		apt-get install gimp-data < "$YES"
		apt-get install gimp-plugin-registry < "$YES"
		apt-get install gimp-date-extras < "$YES"
	fi
	#data backup
	if [[ "$Switch_InstallDeja" = "yes" ]];then
		apt-get install deja-dup < "$YES"
	fi

	#video editor
	if [[ "$Switch_InstallOpenshot" = "yes" ]];then
		apt-get install openshot < "$YES"
	fi

	#some downloaders
	#BT clients
	if [[ "$Switch_InstallVuze" = "yes" ]];then
		apt-get install vuze < "$YES"
	fi
	if [[ "$Switch_InstallDeluge" = "yes" ]];then
		apt-get install deluge < "$YES"
	fi
	#FTP clients
	if [[ "$Switch_InstallFilezilla" = "yes" ]];then
		apt-get install filezilla < "$YES"
	fi
	if [[ "$Switch_InstallGftp" = "yes" ]];then
		apt-get install gftp < "$YES"
	fi
	#FTP server
	if [[ "$Switch_InstallVsftp" = "yes" ]];then
		apt-get install vsftp < "$YES"
	fi
	#wget, multiget, d4x and amule
	if [[ "$Switch_InstallWget" = "yes" ]];then
		apt-get install wget < "$YES"
	fi
	if [[ "$Switch_InstallMultiget" = "yes" ]];then
		apt-get install multiget < "$YES"
	fi
	if [[ "$Switch_InstallD4x" = "yes" ]];then
		apt-get install d4x < "$YES"
	fi
	if [[ "$Switch_InstallAmule" = "yes" ]];then
		apt-get install amule < "$YES"
	fi

	#pdf viewer
	if [[ "$Switch_InstallXpdf" = "yes" ]];then
		apt-get install xpdf < "$YES"
	fi
	#chm viewer
	if [[ "$Switch_InstallChmsee" = "yes" ]];then
		apt-get install chmsee < "$YES"
	fi

	#Google Chrome Browser
	if [[ "$Switch_InstallChrome" = "yes" ]];then
		apt-get install chromium-browser < "$YES"
	fi

	#Star Dictionary
	if [[ "$Switch_InstallStarDict" = "yes" ]];then
		apt-get install stardict < "$YES"
	fi

	#LaTeX
	if [[ "$Switch_InstallTeXLive" = "yes" ]];then
		apt-get install texlive-full < "$YES"
	fi

	#install ia32 libs for x86 in x64 architecture
	if [[ "$Switch_InstallIa32Libs" = "yes" ]];then
		if [ "$arch" = "x86_64" ];then 
			apt-get install ia32-libs* < "$YES"
		fi
	fi

	#install Eclipse
	if [[ "$Switch_InstallEclipse" = "yes" ]];then
		apt-get install eclipse < "$YES"
	fi

	#install Netbeans
	if [[ "$Switch_InstallNetbeans" = "yes" ]];then
		apt-get install netbeans < "$YES"
	fi

	#some games, hope you will like them!
	if [[ "$Switch_InstallWesnoth" = "yes" ]];then
		apt-get install wesnoth < "$YES"
	fi
	if [[ "$Switch_InstallWormux" = "yes" ]];then
		apt-get install wormux < "$YES"
	fi
	if [[ "$Switch_InstallHedgewars" = "yes" ]];then
		apt-get install hedgewars < "$YES"
	fi
	if [[ "$Switch_InstallFrozenBubble" = "yes" ]];then
		apt-get install frozen-bubble < "$YES"
	fi
	if [[ "$Switch_InstallTremulous" = "yes" ]];then
		apt-get install tremulous < "$YES"
	fi
	if [[ "$Switch_InstallNeverball" = "yes" ]];then
		apt-get install neverball < "$YES"
	fi

	#remote desktop client for Linux
	if [[ "$Switch_InstallGnomeRDP" = "yes" ]];then
		apt-get install gnome-rdp < "$YES"
	fi

	#remove garbage
	apt-get autoremove < "$YES"
fi
# install local softwares
if [[ "$Switch_InstallLocalSoftwares" = "yes" ]];then
	#install Adobe Reader for Linux
	if [[ "$Switch_InstallAdobeReader" = "yes" ]];then
		if [ "$ARCH" = "x86_64" ]; then
			dpkg --force-architecture -i softwares/AdbeRdr*_i386linux_*.deb
		else
			dpkg -i softwares/AdbeRdr*_i386linux_*.deb
		fi
	fi
	# install linux QQ
	if [[ "$Switch_InstallLinuxQQ" = "yes" ]];then
		if [[ "$ARCH" = "x86_64" ]];then
			:
		else
			dpkg -i softwares/linuxqq_v*_i386.deb
		fi
	fi

	# install w32codecs for Linux x86
	if [[ "$Switch_InstallW32Codecs" = "yes" ]];then
		if [[ "$ARCH" = "x86_64" ]];then
			:
		else
			dpkg -i softwares/w32codecs_*_i386.deb
		fi
	fi
	# install w64codecs for Linux x86_64
	if [[ "$Switch_InstallW64Codecs" = "yes" ]];then
		if [[ "$ARCH" = "x86_64" ]];then
			dpkg -i softwares/w64codecs_*_amd64.deb	
		fi
	fi

	# install RealPlayer for Linux
	if [[ "$Switch_InstallRealPlayer" = "yes" ]];then
		if [[ "$ARCH" = "x86_64" ]];then
			:
		else
			dpkg -i softwares/RealPlayer11GOLD.deb
		fi
	fi

fi
# install gnome3
if [[ "$Switch_InstallGnome3" = "yes" ]];then
	add-apt-repository ppa:gnome3-team/gnome3
	apt-get update
	apt-get dist-upgrade
	apt-get install gnome-shell < "$YES"
fi

rm -f "$YES"
rm -f "$NO"

echo "Done!"
sleep 1

exit 0
